const request=require("superagent");
const https = require('https');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const util = require('util');
const charset = require("superagent-charset")(request);
const fs=require('fs');
var mysql      = require('mysql');
const assert = require('assert');
var connection = mysql.createConnection({
  host     : '127.0.0.1',
  user     : 'root',
  password : '123456',
  database : 'matter'
});
connection.query = util.promisify(connection.query);
async function getResourceTree(page) {
    var resource = await page._client.send('Page.getResourceTree');
    return resource.frameTree;
}

async function getResourceContent(page, url) {
    const { content, base64Encoded } = await page._client.send(
        'Page.getResourceContent',
        { frameId: String(page.mainFrame()._id), url },
    );
    assert.equal(base64Encoded, true);
    return content;
};
const Agent=[
 	"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
    "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "JUC (Linux; U; 2.3.7; zh-cn; MB200; 320*480) UCWEB7.9.3.103/139/999",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0a1) Gecko/20110623 Firefox/7.0a1 Fennec/7.0a1",
    "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
    "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3",
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7",
    "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10",
    "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
    "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
    "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
    "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36",
];
const timeout={
	response:50000,
	deadline:600000,
}
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
function delay(t){
    return new Promise(resolve => setTimeout(resolve, t))
}

function rnd(n, m){
    var random = Math.floor(Math.random()*(m-n+1)+n);
    return random;
}
function getRndIP(){
    var ip=[];
    for(var i=0;i<4;i++){
    ip.push(rnd(10,250));
    }
    return ip.join(".");
}

const agent_c = new https.Agent({
  rejectUnauthorized: false
});

// process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
//封装请求 实现失败重试
async function requestWithRetry(url, retryCount = 3, timeout = 5000,option) {
  return new Promise((resolve, reject) => {
    let numRetries = 0;
 
    const makeRequest = () => {
      request
        .get(url)
        .set(option)
        .agent(agent_c)
        .timeout(timeout) // 设置超时时间
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          if (error.code === 'ECONNABORTED' && numRetries < retryCount) {
            numRetries += 1;
            console.log(`Request timed out. Retrying... (Attempt ${numRetries})`);
            makeRequest(); // 重试请求
          } else {
            reject(error);
          }
        });
    };
 
    makeRequest();
  });
}
async function getArea(){
    // 需要重写
    // let level_i = 1;
    // let sql=`select * from m_area where id > 0 AND level = `+level_i;
    //     let result=await connection.query(sql);
    //     for (var i = 0; i < result.length; i++) {
    //         var ip=getRndIP();
    //         var agent=Agent[rnd(0,Agent.length-1)];
    //         agent=Agent[0];
    //         var option={
    //             "Host":"www.lnzwfw.gov.cn",
    //             "User-Agent":agent,
    //             "Referer":'https://www.lnzwfw.gov.cn/',
    //             "X-Forwarded-For":ip,
    //         }
    //         let url=`https://www.lnzwfw.gov.cn/matter/areaMenu/list?areaCode=`+result[i]['code'];
    //         console.log(result[i]['id'],result[i]['name'],url);
    //         let res = await requestWithRetry(url, 3, 5000,option).then(response => {
    //             // console.log('Response received:', response);
    //             return response;
    //           }).catch(error => {
    //             console.error('Error:', error);
    //           });
    //         let resData = JSON.parse(res.text).data;
    //         for (var j = 0; j < resData.length; j++) {
    //             await asySql("INSERT INTO m_area SET  ?",{
    //                     'pid' : result[i]['id'],
    //                     'code' : resData[j]['areaCode'],
    //                     'name' : resData[j]['areaName'],
    //                     'level' : level_i+1,
    //                     'url': resData[j]['url']
    //                 }).then(insertId => {
    //                     return insertId;
    //                 }).catch(error => {
    //                   console.error(error); // 异步操作失败，输出错误信息
    //                 });
    //         }
    //         await delay(500);
            
    //     }
    		
}

async function getUnit(){
    // let serverType = 'personal';//个人
    let serverType = 'corporation';//公司
    let sql=`select * from m_area where id > 0 AND pid = 61`;
    let result=await connection.query(sql);
    for (var i = 0; i < result.length; i++) {
            var ip=getRndIP();
            var agent=Agent[rnd(0,Agent.length-1)];
            agent=Agent[0];
            var option={
                "Host":"zwfw.nmg.gov.cn",
                "User-Agent":agent,
                "Referer":'https://zwfw.nmg.gov.cn/',
                "X-Forwarded-For":ip,
            }
            let url=`https://zwfw.nmg.gov.cn/handle_affairs?regionCode=${result[i]['code']}&workType=${serverType}&record=istrue`;
            // let url=`https://zwfw.dl.gov.cn/dlPortal/getCategoryByItem?areacode=${result[i]['code']}&serverType=${serverType}`;
            console.log(result[i]['id'],result[i]['name'],url);
            let res = await requestWithRetry(url, 3, 5000,option).then(response => {
                // console.log('Response received:', response);
                return response;
              }).catch(error => {
                console.error('Error:', error);
              });
            let body=res.text;
            var $ = cheerio.load(body);
            //部门
            $('.VueGeRenAnBuMens:eq(0) li:gt(0)').each(async function(){
                let code = $(this).attr('id');
                let name = $(this).find('a').text();
                console.log(code,name)
                let data = {
                    'area_id' : result[i]['id'],
                    'code' : code,
                    'name' : name
                };
                let checkResult = await connection.query(`select * from m_unit where code = '${code}'`);
                if(checkResult.length>0){
                    console.error('数据已存在', name);
                }else{
                    await asySql("INSERT INTO m_unit SET  ?",{
                        'area_id' : result[i]['id'],
                        'code' : code,
                        'name' : name
                    }).then(insertId => {
                        return insertId;
                    }).catch(error => {
                      console.error(error); // 异步操作失败，输出错误信息
                    });
                }

            })
            
            await delay(1000);
            
        }
        
          
process.exit(0);  
}

async function getMatterList(){
    let serverType = 'personal';//个人
    // let serverType = 'corporation';//公司
    let sql=`select * from m_area where id > 0 AND pid = 61`;
    let result=await connection.query(sql);
    for (var i = 0; i < result.length; i++) {
        //区域id
        let area_id = result[i]['id'];
        //区域编码
        let area_code = result[i]['code'];


        //循环部门爬取 方便目录插入部门
        let unitResult = await connection.query(`select * from m_unit where area_id = ${result[i]['id']}`);
        for (var u = 0; u < unitResult.length; u++) {
            //部门id
            let unit_id = unitResult[u]['id'];
            //部门编码
            let OrgCode = unitResult[u]['code'];

            var ip=getRndIP();
            var agent=Agent[rnd(0,Agent.length-1)];
            agent=Agent[0];
            var option={
                "Host":"zwfw.nmg.gov.cn",
                "User-Agent":agent,
                "Referer":'https://zwfw.nmg.gov.cn/',
                "X-Forwarded-For":ip,
            }
            let url=`https://zwfw.nmg.gov.cn/handle_affairs_serch_new?ItemType=&workType=${serverType}&IsIntoHall=&SubjectClassify=&CurrentPage=1&PageSize=90000&OrgCode=${OrgCode}&RegionCode=${result[i]['code']}&ItemNameLike=&IsOnline=`;
            console.log(result[i]['id'],result[i]['name'],unitResult[u]['name'],url);
            //拿到该区域该部门下的事项列表 
            let res = await requestWithRetry(url, 3, 5000,option).then(response => {
                // console.log('Response received:', response);
                return response;
              }).catch(error => {
                console.error('Error:', error);
              });
            let resData = JSON.parse(res.text);
            resData = resData.itemJsonDataList;
            //循环事项列表
            for (var j = 0; j < resData.length; j++) {
                //目录处理1
                let directory_id = await sevaDirectory(resData[j]['FolderCode'],resData[j]['FolderName'],area_id,unit_id,0);
                
                if(directory_id==0){
                    console.error('目录1id错误')
                    continue;
                }
                //判断ItemList是否为空
                let itemList = resData[j]['ItemList'];
                if(itemList==null){
                    for(var list1_i = 0; list1_i < resData[j]['SubFolder'].length;list1_i++){
                        //目录处理2
                        let directory_id2 = await sevaDirectory(resData[j]['SubFolder'][list1_i]['FolderCode'],resData[j]['SubFolder'][list1_i]['FolderName'],area_id,unit_id,directory_id);
                        if(directory_id2==0){
                            console.error('目录2id错误')
                            continue;
                        }
                        if(resData[j]['SubFolder'][list1_i]['ItemList']==null){

                            for(var list2_i = 0; list2_i < resData[j]['SubFolder'][list1_i]['SubFolder'].length;list2_i++){
                                //目录处理3
                                let directory_id3 = await sevaDirectory(resData[j]['SubFolder'][list1_i]['SubFolder'][list2_i]['FolderCode'],resData[j]['SubFolder'][list1_i]['SubFolder'][list2_i]['FolderName'],area_id,unit_id,directory_id2);
                                if(directory_id3==0){
                                    console.error('目录3id错误')
                                    continue;
                                }
                                //事项处理3
                                itemList = resData[j]['SubFolder'][list1_i]['SubFolder'][list2_i]['ItemList']
                                await saveMatter(itemList,directory_id3,area_id,unit_id);
                            }
                        }else{
                           //事项处理2
                            itemList = resData[j]['SubFolder'][list1_i]['ItemList']
                            await saveMatter(itemList,directory_id2,area_id,unit_id); 
                        }
                        
                    }
                }else{
                    //事项处理1
                    await saveMatter(itemList,directory_id,area_id,unit_id);
                }



            }



        }
        
            
    }
        
          
process.exit(0);  
}

//保存事项
async function saveMatter(itemList,directory_id,area_id,unit_id) {
    if(itemList!=null){
        for(var k = 0; k < itemList.length; k++) {
        let checkResult = await connection.query(`select * from m_matter where ItemID = '${itemList[k]['ItemID']}'`);
        if(checkResult.length>0){
            console.error('事项数据已存在', checkResult[0]['name']);
        }else{
            await asySql("INSERT INTO m_matter SET  ?",{
                    'ItemID' : itemList[k]['ItemID'],
                    'name':itemList[k]['ItemName'],
                    'directory_id':directory_id,
                    'area_id': area_id,
                    'unit_id': unit_id
                }).then(insertId => {
                    return insertId;
                }).catch(error => {
                  console.error(error); // 异步操作失败，输出错误信息
                });
        }
    }
    }
    
}
//保存目录
async function sevaDirectory(FolderCode,FolderName,area_id,unit_id,pid){
    let directory_id = 0;
    let directoryResult = await connection.query(`select * from m_matter_directory where area_id = '${area_id}' and FolderCode = '${FolderCode}'`);
    if(directoryResult.length>0){
        directory_id = directoryResult[0]['id'];
        console.error('目录数据已存在', directoryResult[0]['name']);
    }else{

        directory_id = await asySql("INSERT INTO m_matter_directory SET  ?",{
                'FolderCode' : FolderCode,
                'name' : FolderName,
                'area_id': area_id,
                'unit_id': unit_id,
                'pid':pid
            }).then(insertId => {
                return insertId;
            }).catch(error => {
              console.error(error); // 异步操作失败，输出错误信息
            });
    }
    return directory_id;
}

async function getMatterInfo(){
    //字段
    let field_list = {
        'name':'事项名称',
        'base_code':'基本编码',
        'implement_code':'实施编码',
        'implement_subject':'实施主体',
        'implement_subject_code':'实施主体编码',
        'implement_subject_nature':'实施主体性质',
        'exercise_level':'行使层级',
        'power_source':'权力来源',
        'joint_agency':'联办机构',
        'event_type':'事项类型',
        'limitation':'数量限制',
        'event_status':'事项状态',
        'event_version':'事项版本',
        'service_object':'服务对象',
        'processing_type':'办件类型',
        'scope_application':'通办范围',
        'intermediary_services':'中介服务',
        'intermediary_services_name':'中介服务名称',
        'application_form':'办理形式',
        'is_entry':'是否进驻政务大厅',
        'is_online_office':'是否网办',
        'online_office_depth':'网上办理深度',
        'computer_is_login':'计算机端是否对接单点登录',
        'computer_link':'计算机端在线办理跳转地址',
        'phone_is_login':'移动端是否对接单点登录',
        'phone_link':'移动端办理地址',
        'is_self_service':'是否支持自助终端办理',
        'is_reservation':'是否支持预约办理',
        'is_charge':'是否收费',
        'is_online_payment':'是否支持网上支付',
        'is_express_delivery':'是否支持物流快递',
        'approval_result_type':'审批结果类型',
        'approval_result_name':'审批结果名称',
        'approval_result_sample':'审批结果样本'
    };

    let sql=`select * from m_matter where id > 0 AND base_code IS NULL`;
    let result=await connection.query(sql);
    for (var i = 0; i < result.length; i++) {
        var ip=getRndIP();
        var agent=Agent[rnd(0,Agent.length-1)];
        agent=Agent[0];
        var option={
            "Host":"zwfw.nmg.gov.cn",
            "User-Agent":agent,
            "Referer":'https://zwfw.nmg.gov.cn/',
            "X-Forwarded-For":ip,
        }
        //事项详情页
        //https://zwfw.nmg.gov.cn/handle_affairs_detail?item_detail_id=8a8e936689dec2a4018a2a896c241572&regionCode=150000&highlight=&record=istrue
        let url=`https://zwfw.nmg.gov.cn/handle_affairs_detail?item_detail_id=${result[i]['ItemID']}&regionCode=150000&highlight=&record=istrue`;
        console.log(result[i]['id'],result[i]['name'],url);
       
        let res = await requestWithRetry(url, 3, 8000,option).then(response => {
            // console.log('Response received:', response);
            return response;
          }).catch(error => {
            console.error('Error:', error);
          });
        // let res=await request.get(url).set(option).timeout(timeout).buffer(true);
        let body=res.text;
        var $ = cheerio.load(body);
        let data = {
            'arrive_num':$('.vue-banshi-bot-s-l:eq(0) em:eq(0)').text().trim(),
            'legal_time':$('.vue-banshi-bot-s-l:eq(0) em:eq(1)').text().trim(),
            'promise_time':$('.vue-banshi-bot-s-l:eq(0) em:eq(2)').text().trim(),
        };
        $('.vue-banshi-jian-x-jiben-x tr').each(async function(){
            //遍历th
            $(this).find('th').each(async function(){
                // console.log(1,$(this).text().trim())
                // console.log(2,$(this).next().text().replace(/\s+/g,""))
                let title = $(this).text().trim()
                let value = $(this).next().text().replace(/\s+/g,"");
                if(title=='实施主体性质'){
                    if(value=='法定机关'){
                        value = 1;
                    }else if(value=='授权组织'){
                        value = 2;
                    }else if(value=='受委托组织'){
                        value = 3;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }   
                }
                if(title=='行使层级'){
                    if(value == '自治区级,盟市级,县级'){
                        value = value.split(",");
                    }else if(value == '省级、设区的市级、县级'){
                        value = value.split("、");
                    }else{
                        value = value.split("^");
                    }
                    
                    for (var e_i = 0; e_i < value.length; e_i++) {
                        if(value[e_i]=='国家级'){
                            value[e_i] = 1;
                        }else if(value[e_i]=='自治区级'||value[e_i]=='省级'){
                            value[e_i] = 2;
                        }else if(value[e_i]=='盟市级'){
                            value[e_i] = 3;
                        }else if(value[e_i]=='旗县（区）级'||value[e_i]=='县级'){
                            value[e_i] = 4;
                        }else if(value[e_i]=='镇级'||value[e_i]=='乡级'){
                            value[e_i] = 5;
                        }else if(value[e_i]=='村级'){
                            value[e_i] = 6;
                        }else{
                            value[e_i] = null;
                        }
                    }
                    value = value.join(","); 
                }
                if(title=='事项类型'){
                    if(value=='行政许可'){
                        value = 1;     
                    }else if(value=='行政给付'){
                        value = 2;
                    }else if(value=='行政确认'){
                        value = 3;
                    }else if(value=='行政奖励'){
                        value = 4;
                    }else if(value=='行政裁决'){
                        value = 5;
                    }else if(value=='行政备案'){
                        value = 6;
                    }else if(value=='其它行政权力'||value=='其他行政权力'){
                        value = 7;
                    }else if(value=='公共服务'){
                        value = 8;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='事项状态'){
                    if(value=='发布'){
                        value = 1;
                    }else if(value=='暂停'){
                        value = 2;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='服务对象'){
                    value = value.split("、");
                    for (var s_i = 0; s_i < value.length; s_i++) {
                        if(value[s_i] == '自然人'){
                            value[s_i] = 1;
                        }else if(value[s_i]=='事业法人'){
                            value[s_i] = 2;
                        }else if(value[s_i]=='企业法人'){
                            value[s_i] = 3;
                        }else if(value[s_i]=='非法人企业'){
                            value[s_i] = 4;
                        }else if(value[s_i]=='行政机关'){
                            value[s_i] = 5;
                        }else if(value[s_i]=='社会组织法人'){
                            value[s_i] = 6;
                        }else if(value[s_i]=='其它组织'||value[s_i]=='其他组织'){
                            value[s_i] = 7;
                        }else{
                            console.error('未找到'+title+'：'+value[s_i])
                            return;
                        }
                    }

                    value = value.join(",");
                }
                if(title=='办件类型'){
                    if(value=='承诺件'){
                        value = 1;
                    }else if(value=='即办件'){
                        value = 2;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='办理形式'){
                    value = value.split("、");
                    for (var a_i = 0; a_i < value.length; a_i++) {
                        if(value[a_i] == '网上办理'){
                            value[a_i] = 1;
                        }else if(value[a_i]=='窗口办理'){
                            value[a_i] = 2;
                        }else if(value[a_i]=='快递申请'){
                            value[a_i] = 3;
                        }else{
                            console.error('未找到'+title+'：'+value[a_i])
                            return;
                        }
                    }
                    value = value.join(",");
                }
                if(title=='是否进驻政务大厅'){
                    if(value=='是'||value==''){
                        value = 1;
                    }else if(value=='否'){
                        value = 0;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='是否网办'){
                    if(value=='是'){
                        value = 1;
                    }else if(value=='否'||value==''){
                        value = 0;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='网上办理深度'){
                    value = value.split('查看详情').join('');
                }
                if(title=='计算机端是否对接单点登录'){
                    if(value=='是'){
                        value = 1;
                    }else if(value=='否'||value==''){
                        value = 0;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='计算机端在线办理跳转地址'){
                    let str1 = $(this).next().find('p').attr('onclick');
                    if(str1?.length>0){
                        value = str1.substring(str1.indexOf("('")+2,str1.indexOf("',"));
                    }else{
                        value = '';
                    }
                }
                if(title=='移动端是否对接单点登录'){
                    if(value=='是'){
                        value = 1;
                    }else if(value=='否'||value==''){
                        value = 0;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='移动端办理地址'){
                    let str1 = $(this).next().find('p').attr('onclick');
                    if(str1?.length>0){
                        value = str1.substring(str1.indexOf("('")+2,str1.indexOf("',"));
                    }else{
                        value = '';
                    }
                }
                if(title=='是否支持自助终端办理'){
                    if(value=='是'){
                        value = 1;
                    }else if(value=='否'||value==''){
                        value = 0;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='是否支持预约办理'){
                    value = value.split('查看').join('');
                    if(value=='是'){
                        value = 1;
                    }else if(value=='否'||value==''){
                        value = 0;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='是否收费'){
                    if(value=='是'){
                        value = 1;
                    }else if(value=='否'||value==''){
                        value = 0;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='是否支持网上支付'){
                    if(value=='是'){
                        value = 1;
                    }else if(value=='否'||value==''){
                        value = 0;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='是否支持物流快递'){
                    value = value.substr(0, 1);
                    if(value=='是'){
                        value = 1;
                    }else if(value=='否'||value==''){
                        value = 0;
                    }else{
                        console.error('未找到'+title+'：'+value)
                        return;
                    }
                }
                if(title=='审批结果样本'){
                    let href = $(this).next().find('a').attr('href');
                    if(href?.length>0){
                        href = href.substring(0, href.lastIndexOf("/")+1)
                        let filename = $(this).next().find('a').attr('download');
                        value = 'https://zwfw.nmg.gov.cn'+href+filename;
                    }else{
                        value = '无';
                    }
                }

                //遍历字段 插入data
                for (const key of Object.keys(field_list)) {
                    // console.log(key); // 输出键名
                    // console.log(field_list[key]); // 输出键值
                    if(field_list[key]==title){
                        data[key] = value;
                    }

                }
            })
        })

        //部门
        //根据部门编码查部门表
        let unitResult = await connection.query(`select * from m_unit where code = '${data.implement_subject_code}'`);
        // console.log(unitResult[0]['full_name'])
        let unit_id = 0;
        //判断部门是否已存在
        if(unitResult.length>0){
            //存在
            //判断部门全称是否存在
            if(unitResult[0]['full_name']==null){
                //不存在 插入部门全称
                console.log('插入部门全称',data.implement_subject)
                await connection.query(`update m_unit SET full_name = '${data.implement_subject}' where id = '${unitResult[0]['id']}'`);
            }
            unit_id = unitResult[0]['id'];
        }else{
            //不存在 插入部门
            console.log('插入部门',data.implement_subject)
            let addSql="INSERT INTO m_unit SET  ?";
            let addSqlParams = {
                'area_id':result[i]['area_id'],
                'name':data.implement_subject,
                'code':data.implement_subject_code,
                'full_name':data.implement_subject
            };
            unit_id = await asySql(addSql,addSqlParams).then(insertId => {
                return insertId;
            }).catch(error => {
              console.error(error); // 异步操作失败，输出错误信息
            });

        }
        data['unit_id'] = unit_id;

        await addDb(data,result[i]['id']);
        await delay(200);

    }


}


async function addDb(data,id){
    // console.log(data.nameId)
    //插入数据
    let addSql=`update m_matter SET ? where id = '${id}'`;
    let addSqlParams = data;
    return await new Promise(resolve => {
        connection.query(addSql,addSqlParams,function (err, result) {
            if(err){
             console.log('[INSERT ERROR] - ',err.message);
             return;
            }else{
                console.log('UPDATA ID:',id);
                return resolve(result.insertId);
            }        
            
        });
    })

        
}

//插入数据库
async function asySql(addSql,addSqlParams){
    return await new Promise(resolve => {
        connection.query(addSql,addSqlParams,function (err, result) {
            if(err){
             console.log('[INSERT ERROR] - ',err.message);
             return;
            }else{
                console.log('INSERT ID:',result.insertId);
                return resolve(result.insertId);
            }        
            
        });
    })
        
}


//内蒙古区域 内蒙古需重写 或使用nmg_area.js 现已全部获取
// getArea();

//部门 可用 已获取：内蒙古自治区、科左中、乌拉特中旗
// getUnit();

//事项列表
// getMatterList();
//事项详情 内蒙古需重写 或使用nmg_matter_info.js和nmg_matter_update.js搭配使用
getMatterInfo();